using Microsoft.AspNetCore.Mvc;
using SuperSocket;
using SuperSocket.WebSocket.Server;

namespace WebSocketServerAspCoreApi.Controllers;

[ApiController]
[Route("[controller]")]
public sealed class WebSocketController : ControllerBase
{
    private readonly ILogger<WebSocketController> _logger;
    private readonly ISessionContainer _sessionContainer;

    public WebSocketController(ILogger<WebSocketController> logger, ISessionContainer sessionContainer)
    {
        _logger = logger;
        _sessionContainer = sessionContainer;
    }

    [HttpGet]
    [Route("/send/sessionId={sessionId:Guid}")]
    public async ValueTask<IActionResult> SendAsync(Guid sessionId)
    {
        var session = _sessionContainer.GetSessionByID(sessionId.ToString());

        if (session is not WebSocketSession webSocketSession)
            return NotFound("session is not online");

        await webSocketSession.SendAsync($"ADD {Properties.Resources.测试数据}");

        return Ok();
    }

    [HttpGet]
    [Route("/send/first")]
    public async ValueTask<IActionResult> SendFirstAsync()
    {
        var session = _sessionContainer.GetSessions<WebSocketSession>().FirstOrDefault();

        if (session is null)
            return NotFound("session is not online");

        await session.SendAsync(Properties.Resources.测试数据);

        return Ok();
    }

    [HttpGet]
    public IActionResult All()
    {
        var session = _sessionContainer.GetSessions<WebSocketSession>();

        return Ok(session);
    }
}